package top.drinyeit.weilong.presenter;

import com.google.gson.Gson;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;

import java.util.Map;

import top.drinyeit.weilong.activity.EnterpriseCertificationActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.model.AddressDetailJsonBean;
import top.drinyeit.weilong.model.AuthenticationDetailBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/8.
 */

public class EnterpriseCertificationPresenter {
    private EnterpriseCertificationActivity activity;

    public EnterpriseCertificationPresenter(EnterpriseCertificationActivity activity) {
        this.activity = activity;
    }

    //获取企业认证详情
    public void getCompany() {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        activity.showLoading();
        CommonSubscriber<HttpResponse<AuthenticationDetailBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<AuthenticationDetailBean>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<AuthenticationDetailBean> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.authenticationDetailBean = o.getData();
                    if (activity.authenticationDetailBean.getCompany_phone() == null){
                        activity.authenticationDetailBean.setCompany_phone("");
                    }else {
                        activity.phone_show.setText(activity.authenticationDetailBean.getCompany_phone());
                    }
                    if (activity.authenticationDetailBean.getCompany_name() == null){
                        activity.authenticationDetailBean.setCompany_name("");
                    }else {
                        activity.company_name_show.setText(activity.authenticationDetailBean.getCompany_name());

                    }
                    if (activity.authenticationDetailBean.getLicense_number() == null){
                        activity.authenticationDetailBean.setLicense_number("");
                    }else {
                        activity.number_show.setText(activity.authenticationDetailBean.getLicense_number());
                    }
                    if (activity.authenticationDetailBean.getCompany_address_json() == null){
                        activity.place_show.setText("请选择");
                        activity.authenticationDetailBean.setCompany_address_json(new AddressDetailJsonBean());
                    }else {
                        activity.place_show.setText(activity.authenticationDetailBean.getCompany_address_json().getProvince() + activity.authenticationDetailBean.getCompany_address_json().getCity() +
                                activity.authenticationDetailBean.getCompany_address_json().getRegion());
                    }
                } else {
                    activity.showToastMsg(o.getMessage());
                    activity.authenticationDetailBean = new AuthenticationDetailBean();
                }
            }
        });
        RetrofitMethod.getInstance().getCompany(subscriber, user_id);
    }

    //修改认证信息
    public void editCompany(Map<String, Object> map, final ProvinceBean province, final CityBean city, final DistrictBean district) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.getPlace_show().setText(province.getName() + city.getName() + "市" + (district == null ? "" : district.getName()));
                }
            }
        });
        RetrofitMethod.getInstance().editCompany(subscriber, map);
    }
}
